package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.common.enums.Wx48UseTypeEnum;
import com.fulihui.wgj.dal.mybatis.domain.Wx48NewsInfo;
import com.fulihui.wgj.dal.mybatis.mapper.Wx48NewsInfoMapper;
import com.fulihui.wgj.facade.request.wx48.Wx48NewsInfoPageQueryRequest;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author wang_wx
 * @date 2018-06-20
 */
@Component
public class Wx48NewsInfoRepository extends BaseRepository<Wx48NewsInfo> {

    @Resource
    Wx48NewsInfoMapper wx48NewsInfoMapper;

    /**
     * 更新旧用途为新用途
     *
     * @param appId      公众号appId
     * @param oldUseType 旧用途
     * @param newUseType 新用途
     * @return 更新成功数
     */
    public int updateUseType2New(String appId, Long oldUseType, Long newUseType) {
        return wx48NewsInfoMapper.updateUseType2New(appId, oldUseType, newUseType);
    }

    /**
     * 组装条件
     *
     * @param wx48NewsInfo
     * @return
     */
    @Deprecated
    Example buildExampleUseType(Wx48NewsInfo wx48NewsInfo) {
        Example example = new Example(Wx48NewsInfo.class);
        Example.Criteria criteria = example.createCriteria();
        String appId = wx48NewsInfo.getAppId();
        Long useType = wx48NewsInfo.getUseType();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        if (Objects.nonNull(useType)) {
            criteria.andEqualTo("useType", useType);
        }
        return example;
    }

    /**
     * 查询特定48小时回复
     *
     * @param appId
     * @return
     */
    public Wx48NewsInfo querySpecificReply(String appId) {
        Example example = new Example(Wx48NewsInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("appId", appId);
        criteria.andEqualTo("useType", Wx48UseTypeEnum.specificReply.getValue());
        return wx48NewsInfoMapper.selectOneByExample(example);
    }

    /**
     * 分页查询
     *
     * @param request
     * @return
     */
    public PageInfo<Wx48NewsInfo> queryPageListByRequest(Wx48NewsInfoPageQueryRequest request) {
        return this.queryPageListByExample(buildExample(request), request.getPage(), request.getRows());
    }

    Example buildExample(Wx48NewsInfoPageQueryRequest request) {
        Example example = new Example(Wx48NewsInfo.class);
        Example.Criteria criteria = example.createCriteria();
        String appId = request.getAppId();
        String title = request.getTitle();
        Long useType = request.getUseType();
        if (StringUtils.isNotBlank(appId)) {
            criteria.andEqualTo("appId", appId);
        }
        if (StringUtils.isNotBlank(title)) {
            criteria.andLike("title", "%" + title + "%");
        }
        if (Objects.nonNull(useType)) {
            criteria.andEqualTo("useType", useType);
        }
        example.orderBy("gmtModified").desc();
        return example;
    }
}
